package sjtu.pirate.report.simple;

import java.io.IOException;
import java.util.List;

import sjtu.pirate.analyzer.PirateAnalyzer;
import sjtu.pirate.format.Form;
import sjtu.pirate.format.FormResult;
import sjtu.pirate.format.Input;
import sjtu.pirate.format.FormResult.ResultType;
import sjtu.pirate.report.ReportTemplate;
import sjtu.pirate.util.FormHelper;
import sjtu.pirate.util.JodaTimeUtil;
import biz.source_code.miniTemplator.MiniTemplator.TemplateSyntaxException;
/**
 * @author wilbur.ma@qq.com
 * @date 2011.05.31
 */
public class FormsTemplate extends ReportTemplate {
	private List<FormResult> formResultList;
	
	public FormsTemplate(String templateFile, String reportDir, 
			PirateAnalyzer analyzer, List<FormResult> formResultList) 
			throws TemplateSyntaxException, IOException {
		super(templateFile, reportDir, "", analyzer);
		this.formResultList = formResultList;
	}
	
	@Override
	protected void setTemplateParams() {
		int caseNum = 0;
		for (FormResult fr : formResultList) {
			Form form = fr.getForm();
			setVar("case_num", ++caseNum);
			if (fr.getResultType() == ResultType.OK) {
				setVar("form_state", "form_succeed");
				setVar("state_str", "No error found");
			} else {
				setVar("form_state", "form_fail");
				setVar("state_str", "Error found");
			}
			stdPath = "/detail/" + FormHelper.buildFormLink(form);
			setVar("form_name", form.getName());
			setVar("form_method", form.getMethod().toString());
			for (Input inp : form.getInputList()) {
				setVar("input_name", inp.getName());
				setVar("input_value", inp.getValue());
				addBlock("inputListBlock");
			}
			setVar("action_uri", form.getActionURI().toString());
			setVar("return_code", "" + fr.getReturnCode());
			setVar("start_time", JodaTimeUtil.formatTime(fr.getStartTime()));
			setVar("end_time", JodaTimeUtil.formatTime(fr.getEndTime()));
			addBlock("formListBlock");
		}
	}
}
